<?php
/**
 * 品鉴卡商家端
 */
namespace app\home\controller;

use think\Controller;
use think\Lang;
use think\Request;

class Sellerpinjian extends BaseSeller
{
    public function _initialize()
    {
        parent::_initialize();
        Lang::load(APP_PATH . 'home/lang/'.config('default_lang').'/sellergroupbuy.lang.php');
        //检查抢购功能是否开启
        if (intval(config('groupbuy_allow')) !== 1) {
            $this->error(lang('groupbuy_unavailable'), 'seller/index');
        }
    }

    /**
     * 品鉴卡列表展示
     * @return \think\Response
     */
    public function index()
    {
        $activity_model = model('Tasting');
        //活动为商品活动，并且为开启状态
        $condition = array();
        $condition['tasting_state'] = 1;
        $condition['tasting_startdate'] = array('elt',TIMESTAMP);
        $condition['tasting_enddate']   = array('egt',TIMESTAMP);

        $activity_list = $activity_model->gettastingList($condition, 10 , 'tasting_id desc');
        $this->assign('activity_list', $activity_list);
        $this->assign('show_page', $activity_model->page_info->render());
        $this->setSellerCurMenu('Sellerpinjian');
        $this->setSellerCurItem('index');

        return  $this->fetch($this->template_dir . 'index');
    }

    /**
     * 参与“品鉴”活动
     */
    public function pinjian_apply()
    {
        //根据活动编号查询活动信息
        $activity_id = intval(input('param.activity_id'));
        if ($activity_id <= 0) $this->error(lang('param_error'), 'Sellerpinjian/index');
        $tastingModel = model('Tasting');
        $tastingGoodsModel = model('Tastinggoods');

        $activity_info = $tastingModel->getOnetastingById($activity_id);
        //活动类型必须是商品并且活动没有关闭并且活动进行中
        if (empty($activity_info) || !in_array($activity_info['tasting_type'],[1,2]) || $activity_info['tasting_state'] != 1 || $activity_info['tasting_startdate'] > time() || $activity_info['tasting_enddate'] < time()) {
            $this->error(lang('pinjian_activity_not_exists'), 'Sellerpinjian/index');
        }
        $this->assign('activity_info', $activity_info);
        #获取当前活动,该商家提交的商品
        $condition = array();
        $condition['tasting_id'] = $activity_id;
        $activitydetail_list = $tastingGoodsModel->getGoodsJoinList($condition);
        //构造通过与审核中商品的编号数组,以便在下方待选列表中,不显示这些内容
        $item_ids = array();
        if (is_array($activitydetail_list) and !empty($activitydetail_list)) {
            foreach ($activitydetail_list as $k => $v) $item_ids[] = $v['goods_id'];
        }

        $this->assign('activitydetail_list', $activitydetail_list);
        //根据查询条件查询商品列表
        $condition = array();
        if (input('param.gc_id') != '') $condition['gc_id'] = intval(input('param.gc_id'));
        if (input('param.brand_id') != '') $condition['brand_id'] = intval(input('param.brand_id'));
        if (trim(input('param.name')) != '') $condition['goods_name'] = array('like', '%' . trim(input('param.name')) . '%');
        if (!empty($item_ids)) $condition['goods_id'] = array('not in', $item_ids);

        $condition['store_id'] = session('store_id');
        $goods_model = model('goods');
        $goods_list = $goods_model->getGoodsOnlineList($condition, '*', 10);

        $this->assign('goods_list', $goods_list);
        $this->assign('show_page', $goods_model->page_info->render());

        $this->assign('search', input('param.get'));
        $this->setSellerCurMenu('Sellerpinjian');
        $this->setSellerCurItem('pinjian_apply');
        return $this->fetch($this->template_dir.'pinjian_apply');
    }
    /**
     * 品鉴活动申请商品
     */
    public function activity_apply_save()
    {
        //判断页面参数
        $item_id_array  = input('post.item_id/a');#获取POST数组,商品ID字符串
        $activity_id    = intval(input('post.activity_id'));

        if (empty($item_id_array)) ds_json_encode(10001,lang('store_activity_choose_goods'));
        if ($activity_id <= 0) ds_json_encode(10001,lang('param_error'));
        //根据页面参数查询活动内容信息，如果不存在则添加，存在则根据状态进行修改
        $tastingModel = model('Tasting');
        $activity_info = $tastingModel->getOnetastingById($activity_id);
        //活动类型必须是商品并且活动没有关闭并且活动进行中
        if (empty($activity_info) || !in_array($activity_info['tasting_type'],[1,2]) || $activity_info['tasting_state'] != 1 || $activity_info['tasting_startdate'] > time() || $activity_info['tasting_enddate'] < time()) {
            $this->error(lang('pinjian_activity_not_exists'), 'Sellerpinjian/index');
        }
        $tastingGoodsModel = model('Tastinggoods');
        $list = $tastingGoodsModel->gettasting_goodsList(array('store_id' => session('store_id'), 'tasting_id' => $activity_id));
        $ids = array(); //已经存在的活动内容编号
        $ids_state2 = array();  //已经存在的被拒绝的活动编号
        if (is_array($list) and !empty($list)) {
            foreach ($list as $ad) {
                $ids[] = $ad['goods_id'];
                if ($ad['state'] == '2') $ids_state2[] = $ad['tasting_id'];
            }
        }
        //根据查询条件查询商品列表
        foreach ($item_id_array as $item_id) {
            $item_id = intval($item_id);
            if (!in_array($item_id, $ids)) {
                $input = array();
                $input['tasting_id'] = $activity_id;
                $goods = model('goods');
                $item = $goods->getGoodsOnlineInfoByID($item_id);

                if (empty($item) || $item['store_id'] != session('store_id')) continue;

                $input['goods_name']= $item['goods_name'];
                $input['goods_id']  = $item_id;
                $input['store_id']  = $item['store_id'];
                $input['store_name']= $item['store_name'];
                $tastingGoodsModel->addtasting_goods($input);
            }
            elseif (in_array($item_id, $ids_state2)) {
                $input = array();
                $input['activitydetail_state'] = '0';//将重新审核状态去除
                $tastingGoodsModel->edittasting_goods($input, array('goods_id' => $item_id));
            }
        }
        ds_json_encode(10000,lang('store_activity_submitted'));
    }
    /**
     * 用户中心右边，小导航
     *
     * @param string $menu_type 导航类型
     * @param string $menu_key 当前导航的menu_key
     * @return
     */
    protected function getSellerItemList()
    {
        $menu_array = array(
            array(
                'name' => 'index', 'text' => lang('sale_price'),
                'url' => url('Sellerpinjian/index')
            )
        );
        if (request()->action() == 'pinjian_apply') {
            $menu_array[]=
                array(
                    'name' => 'pinjian_apply', 'text' => lang('ds_member_path_join_activity'),
                    'url' => ''
                );
        }
        return $menu_array;
    }
}
